package com.xy.bizportdemo.conversation;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xy.bizportdemo.InstrumentationWrapper;
import com.xy.bizportdemo.R;
import com.xy.bizportdemo.engine.BaseEngine;
import com.xy.bizportdemo.engine.ConversationLoadEngine;
import com.xy.bizportdemo.setting.SettingActivity;

/**
 * 企业信息列表的主界面
 */
public class MainActivity extends FragmentActivity {
    private static final String TAG = "MainActivity";

    private ListView mListView = null;
    private ConversationListAdapter mListAdapter;
    private Handler mHandler = new Handler();
    private Cursor mCursor = null;

    private void loadMsg() {
        new ConversationLoadEngine(getApplicationContext()).startReadSms(new BaseEngine.SmsReadListener() {
            @Override
            public void onSmsReadFinished(Cursor cursor) {
                mCursor = cursor;
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mListAdapter.changeCursor(mCursor);
                    }
                });
            }

            @Override
            public void onSmsReadError(String errorMsg) {
                Log.i(TAG, "onSmsReadError: " + errorMsg);
                // cursor is not available, so that the notification can not be received correctly
            }
        });
    }

    /* huangzhiqiang 20161128 end */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InstrumentationWrapper.replaceInstrumentation(this);
        InstrumentationWrapper.replaceInstrumentation();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.fragment_list_personal);
        mListAdapter = new ConversationListAdapter(getApplicationContext(), mCursor);
        mListView.setAdapter(mListAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ConversationListItem headerView = (ConversationListItem) view.getTag();
                Intent intent = new Intent();
                intent.setClassName(MainActivity.this, "cn.cmcc.online.smsapi.ui.web.SdkWebActivity");
                //intent.setClass(getApplicationContext(), MessageListActivity.class);
                intent.putExtra("phonenum", headerView.mPhoneNumber);
                intent.putExtra("name", headerView.mTvName.getText());
                startActivity(intent);
            }
        });
        mListView.setOnScrollListener(mScrollListener);


        //进入设置
        findViewById(R.id.iv_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mCursor == null) {
            loadMsg();
        }
    }


    AbsListView.OnScrollListener mScrollListener = new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            switch (scrollState) {
                case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                    mListAdapter.SCROLL_STATE_FLING = true;
                    break;
                default:
                    mListAdapter.SCROLL_STATE_FLING = false;
                    mListAdapter.notifyDataSetChanged();
                    break;
            }

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem == 0) {
                if (mListAdapter != null) {
                    mListAdapter.SCROLL_STATE_FLING = false;

                }
            } else if (firstVisibleItem + visibleItemCount == totalItemCount) {
                if (mListAdapter != null) {
                    mListAdapter.SCROLL_STATE_FLING = false;
                }
            }

        }
    };

    @Override
    protected void onDestroy() {
        if (mCursor != null) {
            mCursor.close();
        }
        super.onDestroy();
    }
}
